/*
 * Copyright (c) 2021
 * User:mallplus多商户商城
 * File:TestController.java
 * Date:2021/01/12 19:29:12
 */

package com.qyine.rlmall.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qyine.rlmall.cms.service.ISysAreaService;
import com.qyine.rlmall.cms.service.ISysSchoolService;
import com.qyine.rlmall.enums.StatusEnum;
import com.qyine.rlmall.fenxiao.mapper.FenxiaoConfigMapper;
import com.qyine.rlmall.pms.service.IPmsFavoriteService;
import com.qyine.rlmall.pms.service.IPmsProductService;
import com.qyine.rlmall.sys.entity.*;
import com.qyine.rlmall.sys.mapper.*;
import com.qyine.rlmall.ums.mapper.UmsEmployInfoMapper;
import com.qyine.rlmall.ums.mapper.UmsRewardLogMapper;
import com.qyine.rlmall.ums.service.IUmsMemberMemberTagRelationService;
import com.qyine.rlmall.ums.service.IUmsMemberService;
import com.qyine.rlmall.ums.service.RedisService;
import com.qyine.rlmall.ums.service.impl.RedisUtil;
import com.qyine.rlmall.utils.CommonResult;
import com.qyine.rlmall.utils.HttpUtils;
import com.qyine.rlmall.cms.entity.CmsSubject;
import com.qyine.rlmall.cms.entity.CmsSubjectCategory;
import com.qyine.rlmall.cms.mapper.CmsSubjectCategoryMapper;
import com.qyine.rlmall.cms.mapper.CmsSubjectMapper;
import com.qyine.rlmall.pms.entity.PmsProduct;
import com.qyine.rlmall.pms.mapper.PmsProductAttributeCategoryMapper;
import com.qyine.rlmall.pms.mapper.PmsProductMapper;
import com.qyine.rlmall.sys.entity.*;
import com.qyine.rlmall.sys.mapper.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Auther: shenzhuan
 * @Date: 2019/4/2 15:02
 * @Description:
 */
@RestController
@Api(tags = "TestController", description = "测试")
@RequestMapping("/test")
public class TestController extends ApiBaseAction {

    @Resource
    CmsSubjectMapper subjectMapper;
    @Resource
    SysSchoolMapper schoolMapper;
    @Resource
    SysAreaMapper sysAreaMapper;
    @Resource
    CmsSubjectCategoryMapper categoryMapper;
    @Resource
    private ISysSchoolService schoolService;
    @Resource
    private IUmsMemberService memberService;
    @Resource
    private ISysAreaService areaService;
    @Resource
    private IUmsMemberMemberTagRelationService memberTagService;
    @Resource
    private UmsRewardLogMapper rewardLogMapper;
    @Resource
    private UmsEmployInfoMapper employInfoMapper;
    @Resource
    private SysStoreMapper storeMapper;
    @Resource
    private PmsProductMapper productMapper;
    @Resource
    private RedisService redisService;
    @Resource
    private IPmsProductService pmsProductService;
    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private IPmsFavoriteService favoriteService;
    @Resource
    private PmsProductAttributeCategoryMapper productAttributeCategoryMapper;

    @ApiOperation("获取会员详情")
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public Object test() {
        PmsProduct product = new PmsProduct();
        product.setStoreName("北京皮包专卖");
        productMapper.update(product, new QueryWrapper<PmsProduct>().eq("store_id", 1));

        product = new PmsProduct();
        product.setStoreName("北京豪车专卖");
        productMapper.update(product, new QueryWrapper<PmsProduct>().eq("store_id", 2));

        product = new PmsProduct();
        product.setStoreName("北京服装专卖");
        productMapper.update(product, new QueryWrapper<PmsProduct>().eq("store_id", 3));

        product = new PmsProduct();
        product.setStoreName("北京手术专卖");
        productMapper.update(product, new QueryWrapper<PmsProduct>().eq("store_id", 4));

        return new CommonResult().success();
    }

    @ApiOperation("获取会员详情")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public Object detail() {
        List<SysArea> areas = sysAreaMapper.selectList(new QueryWrapper<>());
        List<SysSchool> schools = schoolMapper.selectList(new QueryWrapper<>());
        List<CmsSubject> list = subjectMapper.selectList(new QueryWrapper<>());
        for (CmsSubject subject : list) {
            Random r = new Random();
            Integer a = r.nextInt(100);
            Integer c = r.nextInt(3);
            Integer d = r.nextInt(5);
            CmsSubjectCategory cate = categoryMapper.selectById(d);

            if (cate != null) {
                subject.setCategoryName(cate.getName());
                subject.setCategoryId(Long.valueOf(d));
            }

            subject.setType(c);
            Integer b = r.nextInt(100);
            SysSchool school = schools.get(a);
            if (school != null) {
                subject.setSchoolId(school.getId());
                subject.setSchoolName(school.getName());
            } else {
                SysSchool school1 = schools.get(b);
                if (school1 != null) {
                    subject.setSchoolId(school1.getId());
                    subject.setSchoolName(school1.getName());
                }
            }

            SysArea area = areas.get(b);
            if (area != null) {
                subject.setAreaId(area.getId());
                subject.setAreaName(area.getName());
            } else {
                SysArea area1 = areas.get(a);
                if (area1 != null) {
                    subject.setAreaId(area1.getId());
                    subject.setAreaName(area1.getName());
                }
            }
            subjectMapper.updateById(subject);
        }
        return new CommonResult().success();
    }
    @Resource
    SysUserRoleMapper userRoleMapper;
    @Resource
    FenxiaoConfigMapper fenxiaoConfigMapper;

    @Resource
    private SysUserMapper userMapper;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private SysSystemSettingMapper sysSystemSettingMapper;

    @GetMapping("/synGoods")
    public void synGoods(int num) throws IOException {

        String url1 = "http://api.tbk.dingdanxia.com/pdd/recommend";
        String rspStr = HttpUtils.sendGet(url1, "apikey=xBwytZV6u6JAgqSCXuPgkxPeJIBsPp08&&offset=" + num + "&&limit=50");
        JSONObject obj = JSONObject.parseObject(rspStr);

        JSONObject obj1 = JSONObject.parseObject(obj.get("data").toString());
        JSONArray region = obj1.getJSONArray("list");
        for (int i = 0; i < region.size(); i++) {
            try {
                String uu = "http://api.tbk.dingdanxia.com/pdd/goods_detail";
                String rspStr1 = HttpUtils.sendGet(uu, "apikey=xBwytZV6u6JAgqSCXuPgkxPeJIBsPp08&&goods_id_list=" + region.getJSONObject(i).getLong("goods_id"));
                JSONObject obj12 = JSONObject.parseObject(rspStr1);
                JSONObject obj11 = (JSONObject) obj12.getJSONArray("data").get(0);
                PmsProduct g = new PmsProduct();
                g.setName(obj11.get("goods_name").toString());
                g.setSubTitle(obj11.get("goods_desc").toString());
                g.setDescription(obj11.get("goods_desc").toString());
                g.setDetailHtml(obj11.get("goods_desc").toString());
                g.setDetailMobileHtml(obj11.get("goods_desc").toString());
                g.setDetailTitle(obj11.get("goods_desc").toString());
                g.setDetailDesc(obj11.get("goods_desc").toString());
                g.setAreaId(1301L);
                g.setAreaName("石家庄");
                g.setPic(obj11.get("goods_thumbnail_url").toString());
                g.setStoreName(obj11.get("mall_name").toString());
                g.setStoreId(obj11.getInteger("mall_id"));
                String urls = "";
                JSONArray goods_gallery_urls = obj11.getJSONArray("goods_gallery_urls");
                if (obj11.getJSONArray("goods_gallery_urls") != null && obj11.getJSONArray("goods_gallery_urls").size() > 0) {
                    for (int j = 0; j < goods_gallery_urls.size(); j++) {
                        urls = urls + goods_gallery_urls.getString(j) + ",";
                    }
                }
                g.setAlbumPics(urls);
                g.setPrice(obj11.getBigDecimal("min_normal_price"));
                g.setOriginalPrice(obj11.getBigDecimal("min_normal_price"));
                g.setSort(1);
                g.setSale(1);
                g.setStock(100);
                g.setLowStock(0);
                g.setIsVip(0);
                g.setGiftGrowth(0);
                g.setGiftPoint(0);
                g.setIsFenxiao(0);
                g.setPublishStatus(1);
                g.setGiftPoint(0);
                g.setGiftGrowth(0);
                g.setPromotionType(0);
                g.setVerifyStatus(1);
                g.setProductSn("s");
                g.setQsType(1);
                g.setNewStatus(1);
                g.setCreateTime(new Date());

                g.setBrandId(1L);

                g.setProductCategoryId(1L);
                g.setUnit("件");
                g.setProductAttributeCategoryId(1L);
                productMapper.insert(g);
                //   goods.setId(obj11.getLong("goods_id"));
                SysStore entity = storeMapper.selectById(g.getStoreId());
                if (entity == null) {
                    SysSystemSetting mallplusProperties = sysSystemSettingMapper.selectById(1);
                    entity.setStatus(StatusEnum.AuditType.INIT.code());
                    entity.setTryTime(new Date());
                    entity.setCreateTime(new Date());
                    storeMapper.insert(entity);

                    storeMapper.updateById(entity);
                    SysUser user = new SysUser();
                    user.setUsername(entity.getName());
                    user.setNickName(entity.getName());
                    user.setStatus(1);
                    user.setSupplyId(0L);
                    user.setPassword(passwordEncoder.encode("123456"));
                    user.setCreateTime(new Date());
                    user.setIcon(entity.getLogo());

                    user.setNickName(entity.getName());
                    user.setStoreId(entity.getId());
                    user.setStoreName(entity.getName());
                    user.setEmail(entity.getSupportPhone());

                    userMapper.insert(user);

                    SysUserRole userRole = new SysUserRole();
                    userRole.setRoleId(3L);
                    userRole.setAdminId(user.getId());
                    userRoleMapper.insert(userRole);
                }


            } catch (Exception e) {

                continue;
            }
        }


    }

}
